package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user.devicedata;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;


import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.FieldService;

/**
 * 
 * Este Bean es utilizado en las vistas
 * simpleuser/devicedata.xhtml
 */


public class FieldBean {

	private FieldService fieldServ;
	
	public FieldBean(FieldService al) {
		fieldServ = al;
	}

	public String getName(){
		return fieldServ.getName();
		
	}
	public String getValue(){
		try {
			Integer ret = fieldServ.getValue();
			if(ret == null)
				return "devicedata.field.value.novalue";
			return ret.toString();
		}
		catch(Exception e){
		return 	"devicedata.field.value.novalue";
		}
		
	}
	public String getLastdate(){
		try {
			Date ret = fieldServ.getLastDate();
			if(ret == null)
				return "devicedata.field.value.nolastdate";
			return ret.toString();
		}
		catch(Exception e){
		return 	"devicedata.field.value.novalue";
		}
		
	}
	public void dolink(){
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedField", this);
	}
	
	public static List<FieldBean> wrap(
			List<FieldService> fieldsService) {
		ArrayList<FieldBean> ret = new ArrayList<FieldBean>();
		for(FieldService al: fieldsService){
			ret.add(new FieldBean(al));
		}
		return ret;
	}
	

}
